/**
 * SelectNode.cpp
 * Coyright (c) 2009 Xiaofeng Gu. All Rights Reserved.
 * Author : Xiaofeng Gu
 *
 * Creation Data : 2009/08/28
 */

#include "stdafx.h"
#include "SelectNode.h"
#include "Global.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

HRESULT CSelectNode::OnExecute()
{
	HRESULT hr = S_OK;
	hr = __super::OnExecute();
	if (hr != S_OK)
	{
		return hr;
	}

	CGViewer* pViewer = CSKGlobal::GetViewer();
	CSelectNodeProcessCard* pSelectNodeDef = GetBlockInfo<CSelectNodeProcessCard>(SELECTNODEDEF_INSTANCE);
	CKnot* pKnot = pSelectNodeDef->GetParent();

	double Ray[6];
	int iViewPtX = 0;
	int iViewPtY = 0;
	pSelectNodeDef->GetSelectPt(iViewPtX, iViewPtY);
	CVec pt1;
	pViewer->ViewToModelSpace(iViewPtX, iViewPtY, pt1);

	CCamera* pCamera = CSKGlobal::GetCamera();
	CVec pt2 = pCamera->m_Eye;

	Ray[0] = pt2[0];
	Ray[1] = pt2[1];
	Ray[2] = pt2[2];
	Ray[3] = pt1[0];
	Ray[4] = pt1[1];
	Ray[5] = pt1[2];

	CSelectSet* pSelectSet = CSKGlobal::GetSelectSet();
	Nodes* pNodes = pKnot->GetNodes();
	for (Nodes::iterator it = pNodes->begin();
		it != pNodes->end() ; it ++)
	{
		CTransNode* pNode = *it;
		if (pNode->OnSelect(Ray))
		{
			pSelectSet->AddSelect(pNode);
		}
	}

	return S_OK;
}
